package com.molyfun.parents.activity.course.view.courseitem;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.course.bean.CourseMediaBean;
import com.molyfun.parents.dao.entity.SingleDownloadTask;
import com.molyfun.parents.dao.gen.SingleDownloadTaskDao;
import com.molyfun.parents.service.MediaUtils;
import com.molyfun.parents.service.music.MusicStatusCenter;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class CourseMusicView extends BaseCourseView implements View.OnClickListener, MusicStatusCenter.IMusicStatus{

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.course_cell_music_img)
    ImageView mCourseCellImg;
    @InjectView(R.id.course_cell_music_play_state)
    ImageView mPlayState;

    private SingleDownloadTask mSingleCoverDownloadTask, mSingleMusicDownloadTask;

    public CourseMusicView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public CourseMusicView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CourseMusicView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.layout_course_cell_music, this);
        ButterKnife.inject(this);
        mCourseCellImg.setOnClickListener(this);
        mPlayState.setOnClickListener(this);
    }

    /**
     * init view
     *
     */
    public void initData(CourseMediaBean courseMediaBean) {
        super.initData(courseMediaBean);
        if(courseMediaBean == null){
            return;
        }
        if(!TextUtils.isEmpty(courseMediaBean.getImgurl())){
            if(mCourseMediaBean.isOffLineMode()){
                //TODO 本地音乐图片
                List<SingleDownloadTask> singleDownloadTasks = singleDownloadTaskDao.queryBuilder().where(SingleDownloadTaskDao.Properties.Url.eq(courseMediaBean.getImgurl())).build().list();
                if(singleDownloadTasks != null && singleDownloadTasks.size() > 0){
                    mSingleCoverDownloadTask = singleDownloadTasks.get(0);
                }
                if(mSingleCoverDownloadTask != null) {
                    if(!TextUtils.isEmpty(mSingleCoverDownloadTask.getPath())){
                        mCourseCellImg.setImageURI(Uri.fromFile(new File(mSingleCoverDownloadTask.getPath())));
                    }else{
                        mCourseCellImg.setImageResource(R.mipmap.ic_music_holder);
                    }
                }else{
                    mCourseCellImg.setImageResource(R.mipmap.ic_music_holder);
                }
            }else{
                Picasso.with(context)
                        .load(courseMediaBean.getImgurl())
                        .placeholder(R.mipmap.ic_music_holder)
                        .into(mCourseCellImg);
            }
        }else{
            mCourseCellImg.setImageResource(R.mipmap.ic_music_holder);
        }
        //需要根据音乐是否播放，注册音乐播放状态监听器

        mCourseCellImg.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.course_cell_music_img:
            case R.id.course_cell_music_play_state:
                MusicStatusCenter.registerMusicStatusListener(this);
                System.out.println("onClick--->isCurrentPlayingAudio" + MediaUtils.isCurrentPlayingAudio());
                //播放音乐
                if(mCourseMediaBean.getUrl() != null){
                    if(mCourseMediaBean.isOffLineMode()){
                        List<SingleDownloadTask> singleDownloadTasks = singleDownloadTaskDao.queryBuilder().where(SingleDownloadTaskDao.Properties.Url.eq(mCourseMediaBean.getUrl())).build().list();
                        if(singleDownloadTasks != null && singleDownloadTasks.size() > 0){
                            mSingleMusicDownloadTask = singleDownloadTasks.get(0);
                        }
                        if(mSingleMusicDownloadTask != null) {
                            if(!TextUtils.isEmpty(mSingleMusicDownloadTask.getPath())){
                                MediaUtils.startPlayAudio(getContext(), mSingleMusicDownloadTask.getPath());
                            }
                        }
                    }else{
                        MediaUtils.startPlayAudio(context, mCourseMediaBean.getUrl());
                    }
                }
                break;
        }
    }

    @Override
    public void onMusicPause(String musicItem) {
    }

    @Override
    public void onMusicPlay(String musicItem) {

        if(mCourseMediaBean.isOffLineMode()){
            if(!musicItem.equals(mSingleMusicDownloadTask.getPath())){
                mPlayState.setImageResource(R.mipmap.ic_music_play);
            }else{
                mPlayState.setImageResource(R.mipmap.ic_music_stop);
            }

        }else{
            if(!musicItem.equals(mCourseMediaBean.getUrl())){
                mPlayState.setImageResource(R.mipmap.ic_music_play);
            }else{
                mPlayState.setImageResource(R.mipmap.ic_music_stop);
            }
        }
    }

    @Override
    public void onMusicStop(String musicItem) {
        System.out.println("onMusicStop--->" + musicItem);
        if(mCourseMediaBean.isOffLineMode()){
            if(musicItem.equals(mSingleMusicDownloadTask.getPath())){
                mPlayState.setImageResource(R.mipmap.ic_music_play);
            }
        }else{
            if(musicItem.equals(mCourseMediaBean.getUrl())){
                mPlayState.setImageResource(R.mipmap.ic_music_play);
            }
        }
    }

}